<?php
class Test_CV_Array extends UnitTestCase{
	function test_dict(){
		$array = array(
			'key1' => 'value1',
			'key2',
			'key3',
			'key4' => 'value4',
		);
		$expected_result = array(
			'key1' => 'value1',
			'key2' => 'default',
			'key3' => 'default',
			'key4' => 'value4',
		);
		$this->assertIdentical( $expected_result, CV_Array::dict($array, 'default') );
	}
	
	private $_group_by = array(
		0 => array( 'f 1' => 'z', 'f 2' => 'b', 'f 3' => 'k', ),	
		1 => array( 'f 2' => 'x', 'f 3' => 'd', 'f 1' => 'a', ),	
		2 => array( 'f 1' => 'a', 'f 2' => 'b', 'f 3' => 'c', ),	
		3 => array( 'f 1' => 'z', 'f 2' => 'b', 'f 3' => 'l', ),	
		4 => array( 'f 1' => 'a', 'f 2' => 'b', 'f 3' => 'e', ),	
	);
		
	function test_group_by_one_field(){
		$expected_result = array(
			'z' => array(
				0 => array( 'f 1' => 'z', 'f 2' => 'b', 'f 3' => 'k', ),	
				3 => array( 'f 1' => 'z', 'f 2' => 'b', 'f 3' => 'l', ),
			),
			'a' => array(
				1 => array( 'f 2' => 'x', 'f 3' => 'd', 'f 1' => 'a', ),	
				2 => array( 'f 1' => 'a', 'f 2' => 'b', 'f 3' => 'c', ),	
				4 => array( 'f 1' => 'a', 'f 2' => 'b', 'f 3' => 'e', ),
			)	
		);
		$fields = 'f 1';
		$this->assertIdentical( $expected_result, CV_Array::group_by($this->_group_by, $fields) );
		
		$fields = array('f 1');
		$this->assertIdentical( $expected_result, CV_Array::group_by($this->_group_by, $fields) );
	}
		
	function test_group_by_several_fields(){
		$expected_result = array(
			'z' => array(
				'b' => array(
					0 => array( 'f 1' => 'z', 'f 2' => 'b', 'f 3' => 'k', ),	
					3 => array( 'f 1' => 'z', 'f 2' => 'b', 'f 3' => 'l', ),
				),
			),
			'a' => array(
				'x' => array(
					1 => array( 'f 2' => 'x', 'f 3' => 'd', 'f 1' => 'a', ),
				),
				'b' => array(	
					2 => array( 'f 1' => 'a', 'f 2' => 'b', 'f 3' => 'c', ),	
					4 => array( 'f 1' => 'a', 'f 2' => 'b', 'f 3' => 'e', ),
				)
			)	
		);
		$fields = array( 'f 1', 'f 2' );
		$this->assertIdentical( $expected_result, CV_Array::group_by($this->_group_by, $fields) );
	}
	function test_reindex_by_one_field(){
		$expected_result = array(
			'k' => array( 'f 1' => 'z', 'f 2' => 'b', 'f 3' => 'k', ),	
			'd' => array( 'f 2' => 'x', 'f 3' => 'd', 'f 1' => 'a', ),	
			'c' => array( 'f 1' => 'a', 'f 2' => 'b', 'f 3' => 'c', ),	
			'l' => array( 'f 1' => 'z', 'f 2' => 'b', 'f 3' => 'l', ),	
			'e' => array( 'f 1' => 'a', 'f 2' => 'b', 'f 3' => 'e', ),	
		);
		$this->assertIdentical( $expected_result, CV_Array::reindex($this->_group_by, 'f 3') );
	}
	function test_reindex_by_several_fields(){
		$expected_result = array(
			'b' => array(
				'k' => array( 'f 1' => 'z', 'f 2' => 'b', 'f 3' => 'k', ),	
				'c' => array( 'f 1' => 'a', 'f 2' => 'b', 'f 3' => 'c', ),	
				'l' => array( 'f 1' => 'z', 'f 2' => 'b', 'f 3' => 'l', ),	
				'e' => array( 'f 1' => 'a', 'f 2' => 'b', 'f 3' => 'e', ),
			),	
			'x' => array(
				'd' => array( 'f 2' => 'x', 'f 3' => 'd', 'f 1' => 'a', ),	
			),
		);
		$fields = array( 'f 2', 'f 3' );
		$this->assertIdentical( $expected_result, CV_Array::reindex($this->_group_by, $fields) );
	}
}